<template>
    <section>
        <div class="rule-content">
            <div class="item-title">不可连堂节次</div>
            <ul class="course-no-proceed-list">
                <li>
                    <el-checkbox v-for="(value , key, index) in gapRule" v-model="gapRule[key]" :key="index" :checked="value == 1 " :label="value">{{key}}
                    </el-checkbox>
                </li>
            </ul>
            <button type="button" class="btn-save-rule" @click="saveGap">保存</button>
            <div class="link-subjects">
                <div class="item-title">已设连堂科目</div>
                <el-table class="proceed-course-table" :data="tableDate" v-loading.body="tableLoading">
                    <el-table-column prop="courseName" label="科目"></el-table-column>
                    <el-table-column prop="hour" label="课时"></el-table-column>
                    <el-table-column prop="linkCount" label="连堂数"></el-table-column>
                    <el-table-column prop="singleCount" label="单堂数"></el-table-column>
                </el-table>
            </div>
        </div>
    </section>
</template>

<script>
import _ from 'lodash';
import utils from '../../../assets/js/urlConfig.js';
export default {
    data() {
        return {
            gapRule: {},
            gapChecked: [],
            tableDate: [],
            tableLoading: true,
            taskId: sessionStorage.getItem('taskId') || '91',
        }
    },
    mounted() {
        this.getGapRule();
        this.getTable();
    },
    methods: {
        changeTrueFalse(obj) {
            for (var value in obj) {
                obj[value] = obj[value] == '1' ? true : false;
            }
            return obj;
        },
        changeToString(obj) {
            var clone = _.cloneDeep(obj)
            for (var value in clone) {
                clone[value] = clone[value] ? '1' : '0';
            }
            return clone;
        },
        changeParams(obj) {
            var str = '';
            for (var value in obj) {
                str = str + `${value}=${obj[value]}&`
                obj[value] = obj[value] ? '1' : '0';
            }
            return str.slice(0, str.length - 1);
        },
        //不可连堂节次
        getGapRule() {
            this.$http.get(`${utils.UrlConfig.GapRule}&taskId=${this.taskId}`)
                .then(res => {
                    this.gapRule = this.changeTrueFalse(res.data.bizData);
                }).catch(error => {
                    this.$message.error(error);
                })
        },
        // 修改不可连堂节次
        saveGap() {
            var datas = Object.assign({}, this.changeToString(this.gapRule), { 'taskId': this.taskId });
            this.$http.post(utils.UrlConfig.AddGapRule, this.changeParams(datas))
                .then(res => {
                    if (res.data.rtnCode == '0000000') {
                        this.$message({
                            message: '修改成功！',
                            type: 'success'
                        });
                        this.getGapRule();
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).catch(error => {
                    this.$message.error(error);
                })
        },
        // 获取
        getTable() {
            this.$http.get(`${utils.UrlConfig.LinkList}&taskId=${this.taskId}`)
                .then(res => {
                    this.tableLoading = false;
                    this.tableDate = res.data.bizData;
                }).catch(error => {
                    this.$message.error(error);
                })
        }

    }
}
</script>

<style scoped>

</style>
